package cn.windy.samary.module.shiro.realm;

import cn.windy.samary.module.admin.entity.Account;
import cn.windy.samary.module.admin.mapper.AccountMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;

/**
 * @author zyl
 * @date 2018/5/6 9:58
 */
public class FrontShiroRealm extends AuthorizingRealm {

    @Resource
    private AccountMapper accountMapper;

    public FrontShiroRealm() {
        super();
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String account = (String) token.getPrincipal();
        Account acc = new Account();
        acc.setAccount(account);
        Account account1 = accountMapper.selectOne(acc);
        if (account1 != null) {
            AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(account1.getAccount(), account1.getPassword(),"");
            return authenticationInfo;
        }
        return null;
    }

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

}
